package com.personal.gateway.listener;

import com.personal.gateway.config.ValueConfig;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Objects;

/**
 * @program: FlinkGateway
 * @description: 启动项目 复制k8s配置文件 用作flink启动使用
 * @author: yangliao
 * @create: 2021-09-14
 */
@Slf4j
@Component
public class K8sConfigCopyListener implements ApplicationListener<ContextRefreshedEvent> {

    private static final String K8S_CONFIG_PATH = "/root/.kube/config";

    @Resource
    private ValueConfig valueConfig;

    @Override
    @SneakyThrows
    public void onApplicationEvent(ContextRefreshedEvent event) {
        String osName = System.getProperties().getProperty("os.name");
        if (!Objects.equals("linux", osName.toLowerCase())) {
            log.info("非Linux系统不复制");
            return;
        }
        FileUtils.copyFile(new File(valueConfig.getKubeConfigPath()), new File(K8S_CONFIG_PATH));
    }

}
